import json
from MultiChain import get_multichain_data
from cross_chain_data.code_new.code_scan.etherScan import get_ethScan_data
from cross_chain_data.code_new.code_scan.polygonScan import get_polygonScan_data
from cross_chain_data.code_new.code_scan.arbitrumScan import get_arbitrumScan_data
from cross_chain_data.code_new.code_scan.baseScan import get_baseScan_data
from cross_chain_data.code_new.code_scan.gnosisScan import get_gnosisScan_data
from cross_chain_data.code_new.code_scan.optimisimScan import get_optimisimScan_data
from cross_chain_data.code_new.code_scan.avalancheScan import get_avalanceScan_data
from cross_chain_data.code_new.code_scan.lineaScan import get_lineaScan_data
# 1.从跨链平台multichain抓取数据,并记录下来
multichain_data=get_multichain_data()

# 2.使用字典进行动态的选择调用的函数
chain_function_dict={
    'Ethereum Mainnet':get_ethScan_data,
    'Polygon Mainnet':get_polygonScan_data,
    'Arbitrum One':get_arbitrumScan_data,
    'Base':get_baseScan_data,
    'Gnosis':get_gnosisScan_data,
    'OP Mainnet':get_optimisimScan_data,
    'Avalanche C-Chain':get_avalanceScan_data,
}
# 3.设置最终数据的存放列表
total_data=[]
# 统计链名
chain_name=[]
# 2.遍历跨链平台数据
print("************单链平台数据开始爬取************")
for dict_data_multichain in multichain_data:
    # 设置临时存放数据的字典
    tem_dict = {}
    # 获取源链目标链的链名
    input_chain=dict_data_multichain["fromchainName"]
    output_chain=dict_data_multichain["tochainName"]

    if input_chain not in chain_name:
        chain_name.append(input_chain)
    if output_chain not in chain_name:
        chain_name.append(output_chain)

    print(f"源链:{input_chain}，目标链:{output_chain}")
    if input_chain!=output_chain and input_chain in chain_function_dict and output_chain in chain_function_dict :
        # 先获取源链交易哈希 去掉0x
        source_hash=dict_data_multichain["srcTransactionHash"][2:]
        # 调用单链爬取数据函数
        dict_data_source=chain_function_dict[input_chain](source_hash)
        # 再获取目标链交易哈希
        target_hash=dict_data_multichain["destTransactionHash"][2:]
        # 调用单链爬取数据函数
        dict_data_target= chain_function_dict[output_chain](target_hash)
        # 将数据存放到临时字典中
        tem_dict["source_chain_data"]=dict_data_source
        tem_dict["cross_chain_data"]=dict_data_multichain
        tem_dict["target_chain_data"]=dict_data_target
        # 将完整的一条数据存放到最终的列表中
        total_data.append(tem_dict)
    else:print("源链或者目标链的平台数据目前无法获取！")
    print("******************************")

# # 将最终的数据写入文件中
print(f"全部数据爬取完成，最终总共{len(total_data)}条数据")
json_data=json.dumps(total_data,indent=4)
with open('../data_new/total_data_raw_multichain.json', 'a', encoding='utf-8') as f:
    f.write(json_data)



